/**
 * Created by dukes on 2016/3/14.
 */
class FuckItem extends eui.Component{
    constructor(){
        super();
        this.skinName = "pet.FuckItemSkin";
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouched, this);
    }
    public setState(state){
        this.skin.currentState = state;
    }
    private onTouched(evt:egret.TouchEvent){
        if( this._curType == 0 ){
            EventController.getInstance().dispatchEventWith(EventController.SHOP_ITEM_SELECTED,false,this);
        }else  if( this._curType == 1){
            EventController.getInstance().dispatchEventWith(EventController.WAREHOUSE_ITEM_SELECTED,false,this);
        }else if( this._curType == 2){
            EventController.getInstance().dispatchEventWith(EventController.PRODUCT_ITEM_SELECTED,false,this);
        }
    }
    public updateDisplay(){
        if( this._curType == -1){
            this.img_icon.visible = false;
            this.img_lock.visible = true;
            this._curType = -1;
            return;
        }
        if(this._curType == 0){
            this.img_icon.visible = this.curData != null;
            this.img_lock.visible = this.curData == null ;
            if( this.curData ){
                this.img_icon.visible = true;
                this.img_icon.source =  GameConst.Res_Root + "resource/assets/items/" + this.curData.id + ".png";
            }else{
                this.img_icon.visible = false;
            }
        }else if( this._curType == 1){
            this.img_icon.visible = this.curData != null && this.curData.goodsId != 0;
            if( this.curData){
                if( this.curData.goodsId != 0){
                    this.img_icon.source =  GameConst.Res_Root + "resource/assets/items/" + this.curData.goodsId + ".png";
                }
                if(  this.curData.count != 0){
                    this.lab_count.text = "x"+this.curData.count;
                }else{
                    this.lab_count.text = "";
                }
            }else{
                this.lab_count.text = "";
            }
            this.img_lock.visible = this.curData == null || this.curData.state != 2;
        }else if( this._curType == 2){
            this.img_lock.visible = false;
            this.img_icon.visible = true;
            this.img_icon.source =  GameConst.Res_Root + "resource/assets/items/" + this.curData.goodsId + ".png";
        }else if( this._curType == 3){
            this.img_lock.visible = false;
            this.img_icon.visible = true;
            this.img_icon.source =  GameConst.Res_Root + "resource/assets/items/" + this.curData.goodsId + ".png";
        }else if( this._curType == 4){
            this.img_lock.visible = false;
            this.img_icon.visible = true;
            this.img_icon.source =  GameConst.Res_Root + "resource/assets/items/" + this.curData.id + ".png";
        }
    }
    public curData;
    public goodInfo:GoodInfo;
    private _curType:number;//0 ��Ʒ 1 ����  3 ������Ʒ 4 ��Ʒ||Զ����
    public setData(data,type){
        this._curType = type;
        if(type == 0){
            if( data != null)
                this.curData = Model.GetGoodInfoById(data);
            else
                this.curData = null;
        }else if( type == 1){
            // if( data != null)
            //     this.curData = GuildMgr.getInstance().myBoxes[data];
            // else
                this.curData = null;
        }else if( type == 2){
            // this.curData = GuildMgr.getInstance().goodConfig[data];
            this.goodInfo = Model.GetGoodInfoById(data);
        }else if( type == 3){
            this.curData = data;
            this.goodInfo = Model.GetGoodInfoById(data.goodsId);
        }else if( type == 4){
            this.curData = data;
        }
        this._curType = type;
        this.updateDisplay();
    }
    public reset(){
        this.lab_count.text = "";
        this.skin.currentState = "normal";
    }
    private img_icon:eui.Image;
    public img_lock:eui.Image;
    private lab_count:eui.BitmapLabel;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance)
        if( name == "img_icon"){
            this.img_icon = instance;
        }else if(name == "img_lock"){
            this.img_lock = instance;
        }else if(name == "lab_count"){
            this.lab_count = instance;
            this.lab_count.textAlign = "right";
        }
    }
    private static _pool:FuckItem[] = [];
    public static gain(){
        if( FuckItem._pool.length)
            return FuckItem._pool.pop();
        return new FuckItem();
    }
    public static recycle(item:FuckItem){
        if( FuckItem._pool.indexOf(item) == -1){
            item.reset();
            FuckItem._pool.push(item);
        }
    }
}